package com.topvision.common.security;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.topvision.common.enumeration.AuthorizeTypeEnum;
import com.topvision.common.enumeration.YesNo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @author shengwm
 * @version V0.0.1
 * @Title: UserInfo
 * @Package com.topvision.security.security
 * @Description: TODO
 * @date 2017/5/6
 */
public class UserInfo implements UserDetails, Serializable {
    // 用户主键ID
    private Long id;
    // 用户工号
    private String username;
    // 用户密码
    @JsonIgnore
    private String password;
    // 可用
    private boolean enabled;
    // 用户组
    @JsonIgnore
    private Collection<? extends GrantedAuthority> authorities;

    // 用户真名
    private String name;
    // 用户手机号
    private String mobile;
    // 用户邮箱
    private String email;


    // 授权范围
   // private AuthorizeTypeEnum authorizeType;


    public UserInfo() {
    }

    private UserInfo(String username, List<GrantedAuthority> authorities) {
        this.username = username;
        this.authorities = authorities;
    }

    public static UserInfo create(String username, List<GrantedAuthority> authorities) {
        if (StringUtils.isBlank(username)) throw new IllegalArgumentException("Username is blank: " + username);
        return new UserInfo(username, authorities);
    }

    public static UserInfo create(Long id, String username, String password,boolean enabled, String name, String mobile, String email, List<GrantedAuthority> authorities) {
        if (StringUtils.isBlank(username)) throw new IllegalArgumentException("Username is blank: " + username);
        return new UserInfo(id, username,password, true, authorities, name, mobile, email);
    }


    public UserInfo(Long id, String username, String password,boolean enabled, Collection<? extends GrantedAuthority> authorities, String name, String mobile, String email) {
        this.id = id;
        this.username = username;
        this.password=password;
        this.enabled = enabled;
        this.authorities = authorities;
        this.name = name;
        this.mobile = mobile;
        this.email = email;

    }

    public UserInfo(Long id, String username, String password, boolean enabled) {
        super();
        this.id = id;
        this.username = username;
        this.password = password;
        this.enabled = enabled;
    }

    public UserInfo(Long id, String username, String password, boolean enabled,
                    Collection<? extends GrantedAuthority> authorities) {
        super();
        this.id = id;
        this.username = username;
        this.password = password;
        this.enabled = enabled;
        this.authorities = authorities;
    }

    public Long getId() {
        return this.id;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }


   /* public AuthorizeTypeEnum getAuthorizeType() {
        return authorizeType;
    }

    public void setAuthorizeType(AuthorizeTypeEnum authorizeType) {
        this.authorizeType = authorizeType;
    }*/
}
